package Sunday;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

/**
 * 此应用很简陋，只支持两篇文章的简单查重，我们可以对其进行扩展：
 * 1.实现一对多的查重查询
 * 2.实现可查看重复位置的功能
 */

public class ApplicationDemo {
    public static void main(String[] args) {
        Window test=new Window("文章查重应用",10,390,400,590);
    }
}

class Window extends JFrame{
    JTextField filed;
    JTextField filed02;
    JTextField input;
    JTextArea output;
    JButton browse;
    JButton browse02;
    JButton sure;
    JPanel view;
    private File openFile;
    private File openFile02;
    private FileInputStream fileInputStream; //字节文件输入流
    private FileOutputStream fileOutputStream; //字节文件输出流
    private OutputStreamWriter outputStreamWriter; //字符文件输出流

    public Window(String s,int x,int y,int w,int h)
    {
        init(s);
        setLocation(x,y);
        setSize(w,h);
        setVisible(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }
    void init(String s)
    {
        setLayout(new FlowLayout());
        setTitle(s);
        filed = new JTextField(20);
        filed02=new JTextField(20);
        input=new JTextField(20);
        output=new JTextArea(10,30);
        sure=new JButton("确定");
        browse=new JButton("Browse01");
        browse02=new JButton("Browse02");
        add(filed);
        add(browse);
        add(filed02);
        add(browse02);
        add(sure);
        add(new JScrollPane(output));
        browse.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser(); //文件选择
                chooser.showOpenDialog(chooser); //打开文件选择窗
                openFile = chooser.getSelectedFile(); //获取选择的文件
                filed.setText(openFile.getPath()); //获取选择文件的路径
            }
        });

        browse02.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser(); //文件选择
                chooser.showOpenDialog(chooser); //打开文件选择窗
                openFile = chooser.getSelectedFile(); //获取选择的文件
                filed02.setText(openFile.getPath()); //获取选择文件的路径
            }
        });

        sure.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                openFile = new File(filed.getText());
                openFile02 = new File(filed02.getText());
                try {
                    //读取主文件
                    if(!openFile.exists()){ //如果文件不存在
                        openFile.createNewFile();//创建文件
                    }
                    fileInputStream = new FileInputStream(openFile); //创建文件输入流
                    byte b[] = new byte[(int) openFile.length()]; //定义文件大小的字节数据
                    fileInputStream.read(b); //将文件数据存储在 b 数组
                    String content = new String(b,"UTF-8"); //将字节数据转换为 UTF-8 编码的字符串
                    //读取副文件
                    if(!openFile02.exists()){ //如果文件不存在
                        openFile02.createNewFile();//创建文件
                    }
                    fileInputStream = new FileInputStream(openFile02); //创建文件输入流
                    byte q[] = new byte[(int) openFile02.length()]; //定义文件大小的字节数据
                    fileInputStream.read(q); //将文件数据存储在 b 数组
                    String content02 = new String(q,"UTF-8"); //将字节数据转换为 UTF-8 编码的字符串
                    //过滤所有标点符号
                    content=content.replaceAll( "[\\pP+~$`^=|<>～｀＄＾＋＝｜＜＞￥×]" , "");
                    content02=content02.replaceAll( "[\\pP+~$`^=|<>～｀＄＾＋＝｜＜＞￥×]" , "");
                    //过滤所有空白字符
                    content=content.replaceAll("\\s*","");
                    content02=content02.replaceAll("\\s*", "");
                     //过滤中文输入的空白字符
                    content =content.replaceAll(String.valueOf(((char)12288)),"");
                    content02 =content02.replaceAll(String.valueOf(((char)12288)),"");

                     //对比计算重复率
                    int length=content02.length();
                    SundayText test=new SundayText();
                    int m=0;
                    int j=0;
                    for(int i=0;i+10<length;i=i+10) {
                        m = test.NewSunday(content, content02.substring(i,i+10));
                        if(m!=-1)
                        {
                            j++;
                        }
                    }
                    double n=(j*10*100)/length;
                    System.out.println(n);
                    String end=String.valueOf(n);
                    end="重复率为"+end+"％";
                    output.setText(end); //文本区显示文件内容
                    fileInputStream.close(); //关闭文件输入流
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        });
    }
}
